#include "esb_config.h"

#include <stdbool.h>
#include "flash_ctrl.h"
#include "nrf52840_bitfields.h"
#include "zephyr/logging/log.h"

LOG_MODULE_REGISTER(ESB_CFG, LOG_LEVEL_DBG);

static struct esb_config esb_cfg = ESB_DEFAULT_CONFIG;
static uint8_t base_addr_0[4] = {0xE7, 0xE7, 0xE7, 0xE7};
static uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
static uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8};

int esb_base_addr_0_read(void)
{
    int ret;
    ret = flash_data_read(ESB_BASE_ADDR_0_ID, (uint8_t *)&base_addr_0, sizeof(base_addr_0));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB base address 0 from flash");
        return ret;
    }
    LOG_DBG("ESB base address 0 read from flash: %02X %02X %02X %02X", base_addr_0[0], base_addr_0[1], base_addr_0[2], base_addr_0[3]);
    return 0;
}

int esb_base_addr_0_write(uint8_t *addr)
{
    int ret;
    memcpy(base_addr_0, addr, sizeof(base_addr_0));
    ret = flash_data_write(ESB_BASE_ADDR_0_ID, (uint8_t *)&base_addr_0, sizeof(base_addr_0));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB base address 0 to flash");
        return ret;
    }
    LOG_DBG("ESB base address 0 written to flash: %02X %02X %02X %02X", base_addr_0[0], base_addr_0[1], base_addr_0[2], base_addr_0[3]);
    return 0;
}

int esb_base_addr_0_get(uint8_t *addr)
{
    memcpy(addr, base_addr_0, sizeof(base_addr_0));
    LOG_DBG("ESB base address 0: %02X %02X %02X %02X", base_addr_0[0], base_addr_0[1], base_addr_0[2], base_addr_0[3]);
    return 0;
}

int esb_base_addr_1_read(void)
{
    int ret;
    ret = flash_data_read(ESB_BASE_ADDR_1_ID, (uint8_t *)&base_addr_1, sizeof(base_addr_1));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB base address 1 from flash");
        return ret;
    }
    LOG_DBG("ESB base address 1 read from flash: %02X %02X %02X %02X", base_addr_1[0], base_addr_1[1], base_addr_1[2], base_addr_1[3]);
    return 0;
}

int esb_base_addr_1_write(uint8_t *addr)
{
    int ret;
    memcpy(base_addr_1, addr, sizeof(base_addr_1));
    ret = flash_data_write(ESB_BASE_ADDR_1_ID, (uint8_t *)&base_addr_1, sizeof(base_addr_1));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB base address 1 to flash");
        return ret;
    }
    LOG_DBG("ESB base address 1 written to flash: %02X %02X %02X %02X", base_addr_1[0], base_addr_1[1], base_addr_1[2], base_addr_1[3]);
    return 0;
}

int esb_base_addr_1_get(uint8_t *addr)
{
    memcpy(addr, base_addr_1, sizeof(base_addr_1));
    LOG_DBG("ESB base address 1: %02X %02X %02X %02X", base_addr_1[0], base_addr_1[1], base_addr_1[2], base_addr_1[3]);
    return 0;
}

int esb_addr_prefix_read(void)
{
    int ret;
    ret = flash_data_read(ESB_PREFIXES_ID, (uint8_t *)&addr_prefix, sizeof(addr_prefix));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB address prefix from flash");
        return ret;
    }
    LOG_DBG("ESB address prefix read from flash: %02X %02X %02X %02X %02X %02X %02X %02X", addr_prefix[0], addr_prefix[1], addr_prefix[2], addr_prefix[3], addr_prefix[4], addr_prefix[5], addr_prefix[6], addr_prefix[7]);
    return 0;
}

int esb_addr_prefix_write(uint8_t *prefix)
{
    int ret;
    memcpy(addr_prefix, prefix, sizeof(addr_prefix));
    ret = flash_data_write(ESB_PREFIXES_ID, (uint8_t *)&addr_prefix, sizeof(addr_prefix));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB address prefix to flash");
        return ret;
    }
    LOG_DBG("ESB address prefix written to flash: %02X %02X %02X %02X %02X %02X %02X %02X", addr_prefix[0], addr_prefix[1], addr_prefix[2], addr_prefix[3], addr_prefix[4], addr_prefix[5], addr_prefix[6], addr_prefix[7]);
    return 0;
}

int esb_addr_prefix_get(uint8_t *prefix)
{
    memcpy(prefix, addr_prefix, sizeof(addr_prefix));
    LOG_DBG("ESB address prefix: %02X %02X %02X %02X %02X %02X %02X %02X", addr_prefix[0], addr_prefix[1], addr_prefix[2], addr_prefix[3], addr_prefix[4], addr_prefix[5], addr_prefix[6], addr_prefix[7]);
    return 0;
}

int esb_mode_read(void)
{
    int ret;
    ret = flash_data_read(ESB_MODE_ID, (uint8_t *)&esb_cfg.mode, sizeof(esb_cfg.mode));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB mode from flash");
        return ret;
    }
    if(esb_cfg.mode == ESB_MODE_PRX)
    {
        LOG_DBG("ESB mode read from flash: PRX");
    }
    else if(esb_cfg.mode == ESB_MODE_PTX)
    {
        LOG_DBG("ESB mode read from flash: PTX");
    }
    else
    {
        LOG_ERR("Invalid ESB mode read from flash");
        return -1;
    }
    
    return 0;
}

int esb_mode_write(enum esb_mode mode)
{
    int ret;
    esb_cfg.mode = mode;
    ret = flash_data_write(ESB_MODE_ID, (uint8_t *)&esb_cfg.mode, sizeof(esb_cfg.mode));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB mode to flash");
        return ret;
    }
    if(esb_cfg.mode == ESB_MODE_PRX)
    {
        LOG_DBG("ESB mode written to flash: PRX");
    }
    else if(esb_cfg.mode == ESB_MODE_PTX)
    {
        LOG_DBG("ESB mode written to flash: PTX");
    }
    else
    {
        LOG_ERR("Invalid ESB mode written to flash");
        return -1;
    }
    return 0;
}

int esb_mode_get(void)
{
    if(esb_cfg.mode == ESB_MODE_PRX)
    {
        LOG_DBG("ESB mode: PRX");
    }
    else if(esb_cfg.mode == ESB_MODE_PTX)
    {
        LOG_DBG("ESB mode: PTX");
    }
    else
    {
        LOG_ERR("Invalid ESB mode");
        return -1;
    }
    return esb_cfg.mode;
}

int esb_bitrate_read(void)
{
    int ret;
    ret = flash_data_read(ESB_BITRATE_ID, (uint8_t *)&esb_cfg.bitrate, sizeof(esb_cfg.bitrate));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB bitrate from flash");
        return ret;
    }
    if(esb_cfg.bitrate == ESB_BITRATE_2MBPS)
    {
        LOG_DBG("ESB bitrate read from flash: 2MBPS");
    }
    else if(esb_cfg.bitrate == ESB_BITRATE_1MBPS)
    {
        LOG_DBG("ESB bitrate read from flash: 1MBPS");
    }
    else if(esb_cfg.bitrate == ESB_BITRATE_1MBPS_BLE)
    {
        LOG_DBG("ESB bitrate read from flash: 1MBPS");
    }
    else if(esb_cfg.bitrate == ESB_BITRATE_2MBPS_BLE)
    {
        LOG_DBG("ESB bitrate read from flash: 1MBPS");
    }
    else
    {
        LOG_ERR("Invalid ESB bitrate read from flash");
        return -1;
    }
    return 0;
}

int esb_bitrate_write(enum esb_bitrate bitrate)
{
    int ret;
    esb_cfg.bitrate = bitrate;
    ret = flash_data_write(ESB_BITRATE_ID, (uint8_t *)&esb_cfg.bitrate, sizeof(esb_cfg.bitrate));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB bitrate to flash");
        return ret;
    }
    if(esb_cfg.bitrate == ESB_BITRATE_2MBPS)
    {
        LOG_DBG("ESB bitrate written to flash: 2MBPS");
    }
    else if(esb_cfg.bitrate == ESB_BITRATE_1MBPS)
    {
        LOG_DBG("ESB bitrate written to flash: 1MBPS");
    }
    else if(esb_cfg.bitrate == ESB_BITRATE_1MBPS_BLE)
    {
        LOG_DBG("ESB bitrate written to flash: 1MBPS");
    }
    else if(esb_cfg.bitrate == ESB_BITRATE_2MBPS_BLE)
    {
        LOG_DBG("ESB bitrate written to flash: 1MBPS");
    }
    else
    {
        LOG_ERR("Invalid ESB bitrate written to flash");
        return -1;
    }
    return 0;
}

int esb_bitrate_get(void)
{
    if(esb_cfg.bitrate == ESB_BITRATE_2MBPS)
    {
        LOG_DBG("ESB bitrate: 2MBPS");
    }
    else if(esb_cfg.bitrate == ESB_BITRATE_1MBPS)
    {
        LOG_DBG("ESB bitrate: 1MBPS");
    }
    else if(esb_cfg.bitrate == ESB_BITRATE_1MBPS_BLE)
    {
        LOG_DBG("ESB bitrate: 1MBPS");
    }
    else if(esb_cfg.bitrate == ESB_BITRATE_2MBPS_BLE)
    {
        LOG_DBG("ESB bitrate: 1MBPS");
    }
    else
    {
        LOG_ERR("Invalid ESB bitrate");
        return -1;
    }
    return esb_cfg.bitrate;
}

int esb_protocol_read(void)
{
    int ret;
    ret = flash_data_read(ESB_PROTOCOL_ID, (uint8_t *)&esb_cfg.protocol, sizeof(esb_cfg.protocol));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB protocol from flash");
        return ret;
    }
    if(esb_cfg.protocol == ESB_PROTOCOL_ESB)
    {
        LOG_DBG("ESB protocol read from flash: ESB");
    }
    else if(esb_cfg.protocol == ESB_PROTOCOL_ESB_DPL)
    {
        LOG_DBG("ESB protocol read from flash: ESB_DPL");
    }
    else
    {
        LOG_ERR("Invalid ESB protocol read from flash");
        return -1;
    }
    return 0;
}

int esb_protocol_write(enum esb_protocol protocol)
{
    int ret;
    esb_cfg.protocol = protocol;
    ret = flash_data_write(ESB_PROTOCOL_ID, (uint8_t *)&esb_cfg.protocol, sizeof(esb_cfg.protocol));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB protocol to flash");
        return ret;
    }
    if(esb_cfg.protocol == ESB_PROTOCOL_ESB)
    {
        LOG_DBG("ESB protocol written to flash: ESB");
    }
    else if(esb_cfg.protocol == ESB_PROTOCOL_ESB_DPL)
    {
        LOG_DBG("ESB protocol written to flash: ESB_DPL");
    }
    else
    {
        LOG_ERR("Invalid ESB protocol written to flash");
        return -1;
    }
    return 0;
}

int esb_protocol_get(void)
{
    if(esb_cfg.protocol == ESB_PROTOCOL_ESB)
    {
        LOG_DBG("ESB protocol: ESB");
    }
    else if(esb_cfg.protocol == ESB_PROTOCOL_ESB_DPL)
    {
        LOG_DBG("ESB protocol: ESB_DPL");
    }
    else
    {
        LOG_ERR("Invalid ESB protocol");
        return -1;
    }
    return esb_cfg.protocol;
}

int esb_retransmit_delay_read(void)
{
    int ret;
    ret = flash_data_read(ESB_RETRANSMIT_DELAY_ID, (uint8_t *)&esb_cfg.retransmit_delay, sizeof(esb_cfg.retransmit_delay));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB retransmit delay from flash");
        return ret;
    }
    LOG_DBG("ESB retransmit delay read from flash: %d", esb_cfg.retransmit_delay);
    return 0;
}

int esb_retransmit_delay_write(uint32_t retransmit_delay)
{
    int ret;
    esb_cfg.retransmit_delay = retransmit_delay;
    ret = flash_data_write(ESB_RETRANSMIT_DELAY_ID, (uint8_t *)&esb_cfg.retransmit_delay, sizeof(esb_cfg.retransmit_delay));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB retransmit delay to flash");
        return ret;
    }
    LOG_DBG("ESB retransmit delay written to flash: %d", esb_cfg.retransmit_delay);
    return 0;
}

int esb_retransmit_delay_get(void)
{
    LOG_DBG("ESB retransmit delay: %d", esb_cfg.retransmit_delay);
    return esb_cfg.retransmit_delay;
}

int esb_retransmit_count_read(void)
{
    int ret;
    ret = flash_data_read(ESB_RETRANSMIT_COUNT_ID, (uint8_t *)&esb_cfg.retransmit_count, sizeof(esb_cfg.retransmit_count));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB retransmit count from flash");
        return ret;
    }
    LOG_DBG("ESB retransmit count read from flash: %d", esb_cfg.retransmit_count);
    return 0;
}

int esb_retransmit_count_write(uint32_t retransmit_count)
{
    int ret;
    esb_cfg.retransmit_count = retransmit_count;
    ret = flash_data_write(ESB_RETRANSMIT_COUNT_ID, (uint8_t *)&esb_cfg.retransmit_count, sizeof(esb_cfg.retransmit_count));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB retransmit count to flash");
        return ret;
    }
    LOG_DBG("ESB retransmit count written to flash: %d", esb_cfg.retransmit_count);
    return 0;
}

int esb_retransmit_count_get(void)
{
    LOG_DBG("ESB retransmit count: %d", esb_cfg.retransmit_count);
    return esb_cfg.retransmit_count;
}

int esb_selective_auto_ack_read(void)
{
    int ret;
    ret = flash_data_read(ESB_SELECTIVE_AUTO_ACK_ID, (uint8_t *)&esb_cfg.selective_auto_ack, sizeof(esb_cfg.selective_auto_ack));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB selective auto ack from flash");
        return ret;
    }
    if(esb_cfg.selective_auto_ack == true)
    {
        LOG_DBG("ESB selective auto ack read from flash: ENABLED");
    }
    else if(esb_cfg.selective_auto_ack == false)
    {
        LOG_DBG("ESB selective auto ack read from flash: DISABLED");
    }
    else
    {
        LOG_ERR("Invalid ESB selective auto ack read from flash");
        return -1;
    }
  
    return 0;
}

int esb_selective_auto_ack_write(bool selective_auto_ack)
{
    int ret;
    esb_cfg.selective_auto_ack = selective_auto_ack;
    ret = flash_data_write(ESB_SELECTIVE_AUTO_ACK_ID, (uint8_t *)&esb_cfg.selective_auto_ack, sizeof(esb_cfg.selective_auto_ack));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB selective auto ack to flash");
        return ret;
    }
    if(esb_cfg.selective_auto_ack == true)
    {
        LOG_DBG("ESB selective auto ack written to flash: ENABLED");
    }
    else if(esb_cfg.selective_auto_ack == false)
    {
        LOG_DBG("ESB selective auto ack written to flash: DISABLED");
    }
    else
    {
        LOG_ERR("Invalid ESB selective auto ack written to flash");
        return -1;
    }
    return 0;
}

int esb_selective_auto_ack_get(void)
{
    if(esb_cfg.selective_auto_ack == true)
    {
        LOG_DBG("ESB selective auto ack: ENABLED");
    }
    else if(esb_cfg.selective_auto_ack == false)
    {
        LOG_DBG("ESB selective auto ack: DISABLED");
    }
    else
    {
        LOG_ERR("Invalid ESB selective auto ack");
        return -1;
    }
    return esb_cfg.selective_auto_ack;
}

int esb_crc_mode_read(void)
{
    int ret = 0;
    ret = flash_data_read(ESB_CRC_MODE_ID, (uint8_t *)&esb_cfg.crc, sizeof(esb_cfg.crc));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB CRC mode from flash");
        return ret;
    }
    return 0;
}

int esb_crc_mode_write(enum esb_crc crc)
{
    int ret = 0;
    esb_cfg.crc = crc;
    ret = flash_data_write(ESB_CRC_MODE_ID, (uint8_t *)&esb_cfg.crc, sizeof(esb_cfg.crc));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB CRC mode to flash");
        return ret;
    }
    return 0;
}

int esb_crc_mode_get(void)
{
    LOG_DBG("ESB CRC mode: %d", esb_cfg.crc);
    return esb_cfg.crc;
}

int esb_tx_output_power_read(void)
{
    int ret = 0;
    ret = flash_data_read(ESB_TX_OUTPUT_POWER_ID, (uint8_t *)&esb_cfg.tx_output_power, sizeof(esb_cfg.tx_output_power));
    
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB TX output power from flash");
        return ret;
    }

    if(esb_cfg.tx_output_power == ESB_TX_POWER_0DBM)
    {
        LOG_DBG("ESB TX output power read from flash: 0dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG4DBM)
    {
        LOG_DBG("ESB TX output power read from flash: -4dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG8DBM)
    {
        LOG_DBG("ESB TX output power read from flash: -8dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG12DBM)
    {
        LOG_DBG("ESB TX output power read from flash: -12dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG16DBM)
    {
        LOG_DBG("ESB TX output power read from flash: -16dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG20DBM)
    {
        LOG_DBG("ESB TX output power read from flash: -20dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG30DBM)
    {
        LOG_DBG("ESB TX output power read from flash: -30dBm");
    }
    else
    {
        LOG_ERR("Invalid ESB TX output power read from flash");
        return -1;
    }
    return 0;
}

int esb_tx_output_power_write(int8_t tx_output_power)
{
    int ret = 0;
    esb_cfg.tx_output_power = tx_output_power;
    ret = flash_data_write(ESB_TX_OUTPUT_POWER_ID, (uint8_t *)&esb_cfg.tx_output_power, sizeof(esb_cfg.tx_output_power));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB TX output power to flash");
        return ret;
    }

    if(esb_cfg.tx_output_power == ESB_TX_POWER_0DBM)
    {
        LOG_DBG("ESB TX output power written to flash: 0dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG4DBM)
    {
        LOG_DBG("ESB TX output power written to flash: -4dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG8DBM)
    {
        LOG_DBG("ESB TX output power written to flash: -8dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG12DBM)
    {
        LOG_DBG("ESB TX output power written to flash: -12dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG16DBM)
    {
        LOG_DBG("ESB TX output power written to flash: -16dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG20DBM)
    {
        LOG_DBG("ESB TX output power written to flash: -20dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG30DBM)
    {
        LOG_DBG("ESB TX output power written to flash: -30dBm");
    }
    else
    {
        LOG_ERR("Invalid ESB TX output power written to flash");
        return -1;
    }

    return 0;
}

int esb_tx_output_power_get(void)
{
    if(esb_cfg.tx_output_power == ESB_TX_POWER_0DBM)
    {
        LOG_DBG("ESB TX output power: 0dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG4DBM)
    {
        LOG_DBG("ESB TX output power: -4dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG8DBM)
    {
        LOG_DBG("ESB TX output power: -8dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG12DBM)
    {
        LOG_DBG("ESB TX output power: -12dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG16DBM)
    {
        LOG_DBG("ESB TX output power: -16dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG20DBM)
    {
        LOG_DBG("ESB TX output power: -20dBm");
    }
    else if(esb_cfg.tx_output_power == ESB_TX_POWER_NEG30DBM)
    {
        LOG_DBG("ESB TX output power: -30dBm");
    }
    else
    {
        LOG_ERR("Invalid ESB TX output power");
        return -1;
    }
    return esb_cfg.tx_output_power;
}

int esb_tx_mode_read(void)
{
    int ret = 0;
    ret = flash_data_read(ESB_TX_MODE_ID, (uint8_t *)&esb_cfg.tx_mode, sizeof(esb_cfg.tx_mode));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB TX mode from flash");
        return ret;
    }

    if(esb_cfg.tx_mode == ESB_TXMODE_AUTO)
    {
        LOG_DBG("ESB TX mode read from flash: AUTO");
    }
    else if(esb_cfg.tx_mode == ESB_TXMODE_MANUAL)
    {
        LOG_DBG("ESB TX mode read from flash: MANUAL");
    }
    else if(esb_cfg.tx_mode == ESB_TXMODE_MANUAL_START)
    {
        LOG_DBG("ESB TX mode read from flash: MANUAL_START");
    }
    else
    {
        LOG_ERR("Invalid ESB TX mode read from flash");
        return -1;
    }

    return 0;
}

int esb_tx_mode_write(enum esb_tx_mode tx_mode)
{
    int ret = 0;
    esb_cfg.tx_mode = tx_mode;
    ret = flash_data_write(ESB_TX_MODE_ID, (uint8_t *)&esb_cfg.tx_mode, sizeof(esb_cfg.tx_mode));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB TX mode to flash");
        return ret;
    }

    if(esb_cfg.tx_mode == ESB_TXMODE_AUTO)
    {
        LOG_DBG("ESB TX mode written to flash: AUTO");
    }
    else if(esb_cfg.tx_mode == ESB_TXMODE_MANUAL)
    {
        LOG_DBG("ESB TX mode written to flash: MANUAL");
    }
    else if(esb_cfg.tx_mode == ESB_TXMODE_MANUAL_START)
    {
        LOG_DBG("ESB TX mode written to flash: MANUAL_START");
    }
    else
    {
        LOG_ERR("Invalid ESB TX mode written to flash");
        return -1;
    }

    return 0;
}

int esb_tx_mode_get(void)
{
    LOG_DBG("ESB TX mode: %d", esb_cfg.tx_mode);

    if(esb_cfg.tx_mode == ESB_TXMODE_AUTO)
    {
        LOG_DBG("ESB TX mode: AUTO");
    }
    else if(esb_cfg.tx_mode == ESB_TXMODE_MANUAL)
    {
        LOG_DBG("ESB TX mode: MANUAL");
    }
    else if(esb_cfg.tx_mode == ESB_TXMODE_MANUAL_START)
    {
        LOG_DBG("ESB TX mode: MANUAL_START");
    }
    else
    {
        LOG_ERR("Invalid ESB TX mode");
        return -1;
    }

    return esb_cfg.tx_mode;
}

int esb_payload_length_read(void)
{
    int ret = 0;
    ret = flash_data_read(ESB_PAYLOAD_LENGTH_ID, (uint8_t *)&esb_cfg.payload_length, sizeof(esb_cfg.payload_length));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB payload length from flash");
        return ret;
    }
    LOG_DBG("ESB payload length read from flash: %d", esb_cfg.payload_length);
    return 0;
}

int esb_payload_length_write(uint32_t payload_length)
{
    int ret = 0;
    esb_cfg.payload_length = payload_length;
    ret = flash_data_write(ESB_PAYLOAD_LENGTH_ID, (uint8_t *)&esb_cfg.payload_length, sizeof(esb_cfg.payload_length));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB payload length to flash");
        return ret;
    }
    LOG_DBG("ESB payload length written to flash: %d", esb_cfg.payload_length);
    return 0;
}

int esb_payload_length_get(void)
{
    LOG_DBG("ESB payload length: %d", esb_cfg.payload_length);
    return esb_cfg.payload_length;
}

int esb_use_fast_ramp_up_read(void)
{
    int ret = 0;
    ret = flash_data_read(ESB_USE_FAST_RAMP_UP_ID, (uint8_t *)&esb_cfg.use_fast_ramp_up, sizeof(esb_cfg.use_fast_ramp_up));
    if (ret < 0)
    {
        LOG_ERR("Error reading ESB use fast ramp up from flash");
        return ret;
    }
    return 0;
}

int esb_use_fast_ramp_up_write(bool use_fast_ramp_up)
{
    int ret = 0;
    esb_cfg.use_fast_ramp_up = use_fast_ramp_up;
    ret = flash_data_write(ESB_USE_FAST_RAMP_UP_ID, (uint8_t *)&esb_cfg.use_fast_ramp_up, sizeof(esb_cfg.use_fast_ramp_up));
    if (ret < 0)
    {
        LOG_ERR("Error writing ESB use fast ramp up to flash");
        return ret;
    }
    return 0;
}

int esb_use_fast_ramp_up_get(void)
{
    LOG_DBG("ESB use fast ramp up: %d", esb_cfg.use_fast_ramp_up);
    return esb_cfg.use_fast_ramp_up;
}



int esb_config_test(void)
{   

    return 0;
}